﻿using Ewell.Interface.Common.Model.Common;
using Ewell.Interface.Common.Model.Config;
using Ewell.Interface.Common.Model.Constant;
using Ewell.Interface.Common.Model.GateWay;
using Ewell.Interface.Common.Model.GateWay.Dto;
using Ewell.Interface.Common.Model.JsonConverter;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;

namespace Ewell.Interface.Common.Utils
{
    public static class GateWayMethodExtend
    {
        public static string BaseUrl = ProxyConfig.his_getway_url;
        public static GateWayResult Post(this GateWayMethod gateWayMethod,string paras)
        {
            if (string.IsNullOrWhiteSpace(BaseUrl)) throw new SpeedException("未读取到网关地址!请检查package.json设置!");
            LogHelper.Debug($"请求服务【{gateWayMethod.desc}】");
            string url = BaseUrl + gateWayMethod.baseUrl + "/" + gateWayMethod.methodName;
            return HttpHelper.postBasic(url, paras);
        }

        public static GateWayResult<R> Post<R>(this GateWayMethod gateWayMethod, MessageGateWay paras)
        {
            if (string.IsNullOrWhiteSpace(BaseUrl)) throw new SpeedException("未读取到网关地址!请检查package.json设置!");
            paras.authentication = Newtonsoft.Json.JsonConvert.SerializeObject(LoginInfo.loginUser,new JsonDateTimeConverter());
            paras.uuheader = Newtonsoft.Json.JsonConvert.SerializeObject(LoginInfo.loginUser, new JsonDateTimeConverter());
            paras.paramClass = gateWayMethod.paramClass;
            string url = BaseUrl + gateWayMethod.baseUrl + "/" + gateWayMethod.methodName;
            LogHelper.Debug($"ThreadId-【{Thread.CurrentThread.ManagedThreadId}】请求服务【{gateWayMethod.desc}】");
            return HttpHelper.postBasic<MessageGateWay, R>(url, paras);
        }
        public static async Task<GateWayResult<R>> PostAsync<R>(this GateWayMethod gateWayMethod, MessageGateWay paras)
        {
            return await Task.Run<GateWayResult<R>>(() =>
            {
                return Post<R>(gateWayMethod, paras);
            });
        }
        public static GateWayResult<R> Post<R>(this GateWayMethod gateWayMethod, object paras)
        {
            if (string.IsNullOrWhiteSpace(BaseUrl)) throw new SpeedException("未读取到网关地址!请检查package.json设置!");
            if (gateWayMethod.paramType == DubboParamType.messageheader)
            {
                MessageHeader messageHeader = MessageHeader.CreateParam(paras);
                return gateWayMethod.Post<R>(messageHeader);
            }
            else if(gateWayMethod.paramType == DubboParamType.messageheaderDoamin)
            {
                return gateWayMethod.Post <R> (MessageHeader.CreateDomainParam(paras));
            }
            MessageGateWay messageGateWay = new MessageGateWay();
            messageGateWay.authentication = Newtonsoft.Json.JsonConvert.SerializeObject(LoginInfo.loginUser,new JsonDateTimeConverter());
            messageGateWay.uuheader = Newtonsoft.Json.JsonConvert.SerializeObject(LoginInfo.loginUser, new JsonDateTimeConverter());
            messageGateWay.paramClass = gateWayMethod.paramClass;
            string url = BaseUrl + gateWayMethod.baseUrl + "/" + gateWayMethod.methodName;
            JObject keyValuePairs = JObject.FromObject(messageGateWay);
            keyValuePairs.Add(paras);
            LogHelper.Debug($"ThreadId-【{Thread.CurrentThread.ManagedThreadId}】请求服务【{gateWayMethod.desc}】");
            return HttpHelper.postBasic<R>(url, JsonConvert.SerializeObject(keyValuePairs, new JsonDateTimeConverter()));
        }
        public static async Task<GateWayResult<R>> PostAsync<R>(this GateWayMethod gateWayMethod, object paras)
        {
            return await Task.Run<GateWayResult<R>>(() =>
            {
                return Post<R>(gateWayMethod, paras);
            });
        }
    }
}
